﻿using Sharp.DataTransferObject;
using Sharp.Infrastructure.WebApiClient;
using Sharp.Portal.Mvc;
using Sharp.ServiceContracts;
using Sharp.WebBooter.Areas.Platform.Models;
using System;
using System.Collections.Generic;
using System.Net;
using System.Threading.Tasks;
using System.Web.Mvc;
using Sharp.Infrastructure;

// ReSharper disable Mvc.ViewNotResolved

namespace Sharp.WebBooter.Areas.Platform.Controllers
{
    [Authorize]
    [AutomaticTemplate]
    public class JobController : SharpController
    {
        // GET: Job
        public ActionResult Index()
        {
            return View(new JobIndexModel());
        }

        /// <summary>
        /// 分页数据
        /// </summary>
        /// <param name="parameters"></param>
        /// <returns></returns>
        public ActionResult GetPaged(DataTableParameter parameters)
        {
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                var pageInfo = proxy.Channel.GetJobPaged(parameters.ToPagingFeature());

                return this.Store(pageInfo);
            }
        }

        public async Task<ActionResult> Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            JobDTO dto; IEnumerable<OrgDTO> allOrgs;
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                dto = proxy.Channel.GetJobInfo(id.Value);
                allOrgs = await proxy.Channel.GetOrgs();
            }
            if (dto == null)
            {
                return HttpNotFound();
            }

            var model = dto.To<JobModel>();
            OrgController.Build(allOrgs, model.ParentIdDataSource, null);
            return View(model);
        }
        [Permission("新增", 1, "003003001")]
        public async Task<ActionResult> Create()
        {
            IEnumerable<OrgDTO> allOrgs;
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                allOrgs = await proxy.Channel.GetOrgs();
            }
            var model = new JobModel();
            OrgController.Build(allOrgs, model.ParentIdDataSource, null);
            return View(model);
        }
        [Permission("编辑", 2, "003003002")]
        public async Task<ActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            JobDTO dto;
            IEnumerable<OrgDTO> allOrgs;
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                dto = proxy.Channel.GetJobInfo(id.Value);
                allOrgs = await proxy.Channel.GetOrgs();
            }

            if (dto == null)
            {
                return HttpNotFound();
            }
            var model = dto.To<JobModel>();
            OrgController.Build(allOrgs, model.ParentIdDataSource, null);

            return View(model);
        }

        
        public ActionResult Save(JobModel vm)
        {
            JobDTO dto = vm.To<JobDTO>();

            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                var res = proxy.Channel.SaveJob(dto);
                return this.JsonNet(res);
            }

        }

        
        [Permission("删除", 3, "003003003")]
        public ActionResult Delete(int[] ids)
        {
            Result res;
            using (var proxy = new WebApiClientServiceProxy<IEnterpriseService>())
            {
                res = proxy.Channel.DeleteJob(ids);
            }

            return this.JsonNet(res);
        }
    }
}